package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tanhua.domain.db.SoulQuestionStatus;
import com.tanhua.dubbo.api.SoulQuestionStatusApi;
import com.tanhua.dubbo.mapper.SoulQuestionStatusMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class SoulQuestionStatusApiImpl implements SoulQuestionStatusApi {
    @Autowired
    private SoulQuestionStatusMapper soulQuestionStatusMapper;
    
    
    @Override
    public List<SoulQuestionStatus> findByUserId(Long userId) {
        QueryWrapper<SoulQuestionStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return soulQuestionStatusMapper.selectList(queryWrapper);
        
        
    }
    
    /**
     * 根据用户id和得分保存到用户问卷等级状态表，并指定报告id
     *
     * @param soulQuestionStatus
     */
    @Override
    public void saveReportId(SoulQuestionStatus soulQuestionStatus) {
        
        QueryWrapper<SoulQuestionStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", soulQuestionStatus.getUserId()).eq("level", soulQuestionStatus.getLevel());
        UpdateWrapper<SoulQuestionStatus> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", soulQuestionStatus.getUserId()).eq("level", soulQuestionStatus.getLevel());
        if (soulQuestionStatusMapper.selectOne(queryWrapper) != null) {
            soulQuestionStatusMapper.update(soulQuestionStatus, updateWrapper);
        } else {
            soulQuestionStatusMapper.insert(soulQuestionStatus);
        }
        
        
    }
    
    /**
     * 根据用户的报告id，找到相似的用户id
     *
     * @param userId
     * @param reportId
     * @return
     */
    
    @Override
    public List<Long> findSimilarYou(Long userId, Long reportId) {
        List<Long> similarUserIdList = new ArrayList<>();
        
        QueryWrapper<SoulQuestionStatus> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.eq("report_id", reportId).ne("user_id", userId).orderByDesc("updated");
        List<SoulQuestionStatus> similarYouList = soulQuestionStatusMapper.selectList(queryWrapper);
        
        if (similarYouList != null && similarYouList.size() > 0) {
            for (SoulQuestionStatus similarYou : similarYouList) {
                if (!similarUserIdList.contains(similarYou.getUserId())) {
                    
                    similarUserIdList.add(similarYou.getUserId());
                }
            }
        }
        return similarUserIdList;
    }
    
    /**
     * 解锁下一个等级
     */
    @Override
    public void unlockLevel(SoulQuestionStatus status) {
        QueryWrapper<SoulQuestionStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", status.getUserId()).eq("level", status.getLevel());
        SoulQuestionStatus userStatus = soulQuestionStatusMapper.selectOne(queryWrapper);
        if (userStatus == null) {
            soulQuestionStatusMapper.insert(status);
        } else {
            UpdateWrapper<SoulQuestionStatus> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id", status.getUserId()).eq("level", status.getLevel());
            soulQuestionStatusMapper.update(status, updateWrapper);
        }
    }
}
